package cn.itsource.hrm.controller;

import cn.itsource.hrm.controller.vo.CourseVo;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;

    /**
     * 前端表单的数据中有些大字段我们抽取到了另外的表中，所以分开执行
    * 保存和修改公用的
    * @param courseVo  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseVo courseVo){
        try {
            //如果id不存在，新增
            if(courseVo.getId()!=null){
                courseService.updateById(courseVo);
            }else{
                //存在，修改
                courseService.save(courseVo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.list(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {

        return courseService.queryPage(query);
    }

    /*
    * 课程上线
    * */
    @PostMapping("/online")
    public AjaxResult onlie(@RequestBody List<Long> ids){
        try {
            courseService.online(ids);
            return AjaxResult.me().setMessage("上线成功").setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上线失败"+e.getMessage()).setSuccess(false);
        }

    }



}
